<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/15 11:48
 * Desc: 会员联系地址
 */
class  Service_Api_Member_Linkman extends Contract_Api_Member_Linkman
{
    private $table_name = 'member_linkman';

    private $fields = array('id', 'linkman', 'mobile', 'idcard', 'cardtype', 'sex', 'en_first_name', 'en_last_name', 'nationality', 'cardnumber_expire_date', 'birth');

    public function list(int $mid, array $params = array()): array
    {
        /**
         * @var $card_type
         * @var $page
         * @var $pagesize
         */
        extract($params);

        $builder = DB::select_array($this->fields)
            ->from($this->table_name)
            ->where('memberid', '=', $mid);

        // 证件类型
        if (!empty($card_type)) {
            $builder->where('cardtype', '=', $card_type);
        }

        $total = $builder->execute()->count();

        $builder->order_by('id', 'desc');
        if (!empty($page) && !empty($pagesize)) {
            $offset = St_Page::offset($page, $pagesize);
            $builder->offset($offset)->limit($pagesize);
        }

        $list = $builder->execute()->as_array();

        return array(
            'total' => $total,
            'list' => $list
        );
    }


    public function info(int $mid, int $id)
    {
        $info = DB::select_array($this->fields)
            ->from($this->table_name)
            ->where('memberid', '=', $mid)
            ->where('id', '=', $id)
            ->execute()
            ->current();
        return $info ?: '';
    }


    public function edit(int $mid, array $params = array()): array
    {
        /**
         * @var $linkman
         * @var $mobile
         * @var $idcard
         * @var $cardtype
         * @var $sex
         * @var $id
         */
        extract($params);

        if (!empty($id)) {
            $info = ORM::factory($this->table_name)
                ->where('id', '=', $id)
                ->where('memberid', '=', $mid);
            if (!$info->find()->as_array()['id']) {
                throw new Exception('信息不存在');
            }
        } else {
            $info = ORM::factory($this->table_name);
        }
        $info->memberid = $mid;
        $info->linkman  = $linkman;
        $info->mobile   = $mobile;
        $info->cardtype = $cardtype;
        $info->sex      = $sex;

        // 选填
        $info->idcard   = !empty($idcard) ? $idcard : '';
        if (isset($params['en_first_name'])) {
            $info->en_first_name = $en_first_name ?: '';
        }
        if (isset($params['en_last_name'])) {
            $info->en_last_name = $en_last_name ?: '';
        }
        if (isset($params['nationality'])) {
            $info->nationality = $nationality ?: '';
        }
        if (isset($params['cardnumber_expire_date'])) {
            $info->cardnumber_expire_date = $cardnumber_expire_date ?: '';
        }
        if (isset($params['birth'])) {
            $info->birth = $birth ?: '';
        }

        $info->save();
        if ($info->saved()) {
            $info->reload();
            return array('id' => $info->id);
        } else {
            throw new Exception('保存失败');
        }
    }


    public function delete(int $mid, int $id): bool
    {
        $row = DB::delete($this->table_name)
            ->where('memberid', '=', $mid)
            ->where('id', '=', $id)
            ->execute();
        return (bool)$row;
    }
}
